﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ComLib.ComFun;

namespace ComLib.ComType
{
    /// <summary>
    /// 身份证信息
    /// </summary>
    public class IDCardAll : ICloneable
    {
        public string name = "";     //姓名
        public string sex = "";      //性别
        public string people = "";    //民族，护照识别时此项为空
        public string birthday = "";   //出生日期
        public string address = "";  //地址，在识别护照时导出的是国籍简码
        public string number = "";  //地址，在识别护照时导出的是国籍简码
        public string signdate = "";   //签发日期，在识别护照时导出的是有效期至 
        public string samid = ""; // 安全模块号
        public string validtermOfStart = "";  //有效起始日期，在识别护照时为空
        public string validtermOfEnd = "";  //有效截止日期，在识别护照时为空
        public string HeadImage = "";//头像照片
        public string FrontImage = "";//身份证正面照片
        public string BackImage = "";//身份证反面照片
        public string aac001 = "";  //市系统识别号
        public bool IsManualInputNumberVerify = false;

        protected string _strErrorMessage = "";

        public string ErrorMessage
        {       
            get
            {
                return _strErrorMessage;
            }
        }

        public IDCardAll()
        {

        }

        public IDCardAll(string msg)
        {
            _strErrorMessage = msg;
        }

        public bool CheckCardNumber()
        {
            if (string.IsNullOrEmpty(number))
            {
                _strErrorMessage = "身份证号码为空，可能是芯片信息读取错误";
                return false;
            }
            if (!IDCardCheck.Check(number))
            {
                _strErrorMessage = string.Format("号码{0}校验失败, 可能是芯片读取错误。", number);
                return false;
            }
            return true;
        }

        /// <summary>
        /// 获得称谓
        /// </summary>
        /// <returns></returns>
        public string GetTitle()
        {
            if(name.Length < 2)
            {
                return "xxx";
            }
            string FamilyName = name.Substring(0, 1);
            if(sex == "男")
            {
                return FamilyName + "先生";
            }
            if (sex == "女")
            {
                return FamilyName + "女士";
            }
            return "xxx";
        }

        /// <summary>
        /// 获得省略的证件号码
        /// </summary>
        /// <returns></returns>
        public string GetAbridgedNumber()
        {
            if (number.Length != 18 && number.Length != 15)
            {
                return "xxx xxx xxxx xxxx xxxx";
            }
            //char[] charsArray = number.ToCharArray();
            //if(charsArray.Length == 18)
            //{
            //    for (int i = 6; i < 14;i++ )
            //    {
            //        charsArray[i] = '*';
            //    }
            //    return new string(charsArray);
            //}
            //else 
            //{
            //    string str1 = number.Remove(number.Length - 8, 4);
            //    return str1.Insert(number.Length - 8, "****");
            //}   
			string str1 = number.Remove(number.Length - 4, 4);
            return str1 + "xxxx";        
        }

        public void SetNull()
        {
            name = ""; 
            sex = ""; 
            people = ""; 
            birthday = ""; 
            address = "";  
            number = ""; 
            signdate = "";  
            samid = ""; 
            validtermOfStart = ""; 
            validtermOfEnd = "";
            HeadImage = "";
            FrontImage = "";
            BackImage = "";
            aac001 = "";
            IsManualInputNumberVerify = false;
            _strErrorMessage = "";
        }

        public object Clone()
        {
            IDCardAll clone = new IDCardAll();
            clone._strErrorMessage = _strErrorMessage;
            clone.address = this.address;
            clone.name = this.name;
            clone.validtermOfEnd = this.validtermOfEnd;
            clone.validtermOfStart = this.validtermOfStart;
            clone.samid = this.samid;
            clone.signdate = this.signdate;
            clone.number = this.number;
            clone.birthday = this.birthday;
            clone.people = this.people;
            clone.sex = this.sex;
            clone.address = this.address;
            clone.HeadImage = this.HeadImage;
            clone.FrontImage = this.FrontImage;
            clone.BackImage = this.BackImage;
            clone.aac001 = this.aac001;
            clone.IsManualInputNumberVerify = this.IsManualInputNumberVerify;
            return clone;
        }

        public string[] GetAllValues()
        {
            string[] array = new string[10];
            Action<string, string, int> appendText = (field, value, i) =>
            {
                if (i<0 || i >= array.Length)
                {
                    return;
                }
                array[i] = string.Format("[{0}]: {1};", field, value);
            };
            appendText("姓名", name, 0);
            appendText("性别", sex, 1);
            appendText("民族", people, 2);
            appendText("住址", address, 3);
            appendText("出生年月日", birthday, 4);
            appendText("身份证号码", number, 5);
            appendText("签发机关", signdate, 6);
            appendText("起始日期", validtermOfStart, 7);
            appendText("结束日期", validtermOfEnd, 8);
            appendText("安全模块号", samid, 9);
            return array;
        }
    }
}
